﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.User
{
    public partial class User1 : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        UserController.getController().doDelegeUser(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定省份
        /// </summary>
        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 省份选定出发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }

        /// <summary>
        /// 绑定城市
        /// </summary>
        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定修改的数据
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.User model = UserController.getController().getUserModel(ID);
            //输入框赋值
            tbUID.Text = model.UID;
            tbPWD.Text = model.PWD;
            tbEmail.Text = model.Email;
            tbPoint.Text = model.Point.ToString();
            DropProvinceID.SelectedValue = model.ProvinceID.ToString();
            DropCityID.SelectedValue = model.CityID.ToString();
            tbRealName.Text = model.RealName;
            tbContent.Text = model.Content;

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                UserController.getController().doAdminModifyUser(int.Parse(IDS.Value),
                    tbUID.Text,
                    tbPWD.Text,
                    tbEmail.Text,
                    int.Parse(tbPoint.Text),
                    int.Parse(DropProvinceID.SelectedValue),
                    int.Parse(DropCityID.SelectedValue),
                    tbRealName.Text,
                    tbContent.Text);
            }
            else
            {
                UserController.getController().doAdminAddUser(tbUID.Text,
                   tbPWD.Text,
                   tbEmail.Text,
                   int.Parse(tbPoint.Text),
                   int.Parse(DropProvinceID.SelectedValue),
                   int.Parse(DropCityID.SelectedValue),
                   tbRealName.Text,
                   tbContent.Text);
            }
        }
    }
}